export function bindEvents(shadowRoot, instance) {
    const input = shadowRoot.querySelector('sl-input');
    const button = shadowRoot.querySelector('sl-button');

    // 回车搜索
    input.addEventListener('sl-input', (e) => {
        if (e.target.value.trim()) {
            button.removeAttribute('disabled');
        } else {
            button.setAttribute('disabled', '');
        }
    });

    // 点击搜索
    button.addEventListener('click', () => {
        performSearch(input.value, instance);
    });

    // 回车键搜索
    input.addEventListener('keydown', (e) => {
        if (e.key === 'Enter' && input.value.trim()) {
            performSearch(input.value, instance);
        }
    });
}

export function performSearch(query, instance) {
    if (!query.trim()) return;
    
    // 触发自定义搜索事件
    instance.dispatchEvent(new CustomEvent('search', {
        detail: { query: query.trim() },
        bubbles: true
    }));
    
    // console.log('搜索内容:', query.trim());
}

export function clearSearch(shadowRoot) {
    const input = shadowRoot.querySelector('sl-input');
    if (input) {
        input.value = '';
        const button = shadowRoot.querySelector('sl-button');
        if (button) {
            button.setAttribute('disabled', '');
        }
    }
}

export function focusInput(shadowRoot) {
    const input = shadowRoot.querySelector('sl-input');
    if (input) {
        input.focus();
    }
}